package com.evenmo.hrms.controller.oldController;

import com.evenmo.hrms.pojo.Dept;
import com.evenmo.hrms.pojo.TestAjaxJsonParam;
import com.evenmo.hrms.service.HrmsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/deptMoreCondition")
public class DeptMoreConditonController {
    @Autowired
    HrmsService hrmsService;

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> showAllDepts(Integer pageNumber, Integer pageSize) {

        System.out.println(pageNumber + ":" + pageSize);
        //分页
        PageHelper.startPage(pageNumber, pageSize);
        List<Dept> allDepts = hrmsService.findAllDepts();
        PageInfo pageInfo = new PageInfo(allDepts);
        Map<String, Object> result = new HashMap<>();
        result.put("status", "200");
        result.put("data", pageInfo);
        return result;

    }


    @GetMapping("/info/{id}")
    @ResponseBody
    public Map<String, Object> addDept(Integer id) {
        Map<String, Object> result = new HashMap<>();
        System.out.println(id);
        result.put("data", id);
        result.put("status", 200);
        result.put("message", "添加成功");
        return result;
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addDept(Dept dept) {
        Map<String, Object> result = new HashMap<>();
        hrmsService.addDept(dept);
        result.put("status", 200);
        result.put("message", "添加成功");
        return result;
    }


    // 删除部门
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteDept(Integer id) {
        hrmsService.deleteDept(id);
        Map<String, Object> result = new HashMap<>();
        result.put("status", 200);
        result.put("message", "删除成功");
        return result;
    }


    // 测试ajax传递数组
    // 前端传json对象，ajax设置traditional为true
    @PostMapping("/testArrayParam")
    @ResponseBody
    public Map<String, Object> testAjaxArray(String[] arrayParam) {
        System.out.println("获取的arrayParam是：" + arrayParam);
        Map<String, Object> result = new HashMap<>();
        if (arrayParam != null) {
            for (String s : arrayParam) {
                System.out.println("数组参数项是：" + s);
            }
            result.put("status", 200);
            result.put("data", arrayParam);
            result.put("message", "获取参数成功！");
        } else {
            result.put("status", 500);
            result.put("data", null);
            result.put("message", "传递的参数为空！");
        }

        return result;
    }

    // 测试ajax传递数组
    @PostMapping("/testArrayParam3")
    @ResponseBody
    public Map<String, Object> testAjaxArray3(TestAjaxJsonParam testAjaxJsonParam) {

        String[] arrayParam = testAjaxJsonParam.getArrayParam();
        System.out.println("获取的arrayParam是：" + arrayParam);
        Map<String, Object> result = new HashMap<>();
        if (arrayParam != null) {
            for (String s : arrayParam) {
                System.out.println("数组参数项是：" + s);
            }
            result.put("status", 200);
            result.put("data", arrayParam);
            result.put("message", "获取参数成功！");
        } else {
            result.put("status", 500);
            result.put("data", null);
            result.put("message", "传递的参数为空！");
        }

        return result;
    }

    // 测试ajax传递数组
    @PostMapping("/testArrayParam4")
    @ResponseBody
    public Map<String, Object> testAjaxArray4(HttpServletRequest request) throws IOException {


        // 通过流获取json传递过来的参数
        // 方式一：从流中读取json字符串后再转换成java对象
        BufferedReader reader = request.getReader();
        StringBuffer str = new StringBuffer();
        String inputStr = null;
        while ((inputStr = reader.readLine()) != null) {
            str.append(inputStr);

        }
        System.out.println("获取的参数是：" + str);

        ObjectMapper objectMapper = new ObjectMapper();
        TestAjaxJsonParam testAjaxJsonParam1 = objectMapper.readValue(str.toString(), TestAjaxJsonParam.class);
        System.out.println(testAjaxJsonParam1);
        String[] arrayParam1 = testAjaxJsonParam1.getArrayParam();
        System.out.println("参数是：" + arrayParam1);
        for (String str_param1 : arrayParam1) {
            System.out.println(str_param1);
        }

        // 通过流获取json传递过来的参数
        // 方式二：（推荐）
        // 通过objectMapper的readValue()重载方法传入request得到的BufferReader对象，并传入java对象的Class对象，转换为java对象
//        TestAjaxJsonParam testAjaxJsonParam = objectMapper.readValue(reader, TestAjaxJsonParam.class);
//        System.out.println("arrayParam:");
//        System.out.println("得到的java对象是："+testAjaxJsonParam);
//        String[] arrayParam1 = testAjaxJsonParam.getArrayParam();
//        System.out.println("参数是："+arrayParam1);
//        for (String str_param : arrayParam1) {
//            System.out.println(str_param);
//        }


        String[] arrayParam = arrayParam1;


        System.out.println("获取的arrayParam是：" + arrayParam);

        Map<String, Object> result = new HashMap<>();
        if (arrayParam != null) {
            for (String sp : arrayParam) {
                System.out.println("数组参数项是：" + sp);
            }
            result.put("status", 200);
            result.put("data", arrayParam);
            result.put("message", "获取参数成功！");
        } else {
            result.put("status", 500);
            result.put("data", null);
            result.put("message", "传递的参数为空！");
        }

        return result;
    }

    // 测试ajax传递数组
    @PostMapping("/testArrayParam2")
    @ResponseBody
    public Map<String, Object> testAjaxArray2(@RequestBody TestAjaxJsonParam testAjaxJsonParam) {

        Map<String, Object> result = new HashMap<>();
//        List<String> arrayParam = testAjaxJsonParam.getArrayParam();
        String[] arrayParam = testAjaxJsonParam.getArrayParam();

        System.out.println("获取的arrayParam是：" + arrayParam);
        if (arrayParam != null) {
            for (String s : arrayParam) {
                System.out.println("数组参数项是：" + s);
            }
            result.put("status", 200);
            result.put("data", arrayParam);
            result.put("message", "获取参数成功！");
        } else {
            result.put("status", 500);
            result.put("data", null);
            result.put("message", "传递的参数为空！");
        }

        return result;
    }

    @PostMapping("/timeTest")
    @ResponseBody
    public Map<String, Object> timeParamTest(@RequestBody TestAjaxJsonParam testAjaxJsonParam) {
        Map<String, Object> result = new HashMap<>();

        System.out.println("获取的时间参数是：" + testAjaxJsonParam);
        

        if (testAjaxJsonParam!= null) {
            result.put("status", 200);
            result.put("data", testAjaxJsonParam);
            result.put("message", "获取并处理了时间参数。");
        } else {
            result.put("status", 500);
            result.put("message", "没有获取到时间参数。");
            result.put("data", null);

        }

        return result;
    }

    /**
     * 配置了全局的LocalDateTime转换器后，在方法参数用LocalDateTime才可以接收，否则报错
     * @param localDateTime 接收前端传来的时间格式字符串
     * @return
     */
    @PostMapping("/timeTest2")
    @ResponseBody
    public Map<String, Object> timeParamTest2( LocalDateTime localDateTime) {
        Map<String, Object> result = new HashMap<>();

        System.out.println("获取的时间参数是："+localDateTime);

        // 处理参数
        System.out.println(LocalDateTime.of(2022,2,3,4,5,6));
        //LocalDateTime返回的时间格式是： 2022-02-03T04:05:06

        if (localDateTime!= null) {
            result.put("status", 200);
            result.put("data", localDateTime);
            result.put("message", "获取并处理了时间参数。");
        } else {
            result.put("status", 500);
            result.put("message", "没有获取到时间参数。");
            result.put("data", null);

        }

        return result;
    }




    // 查询部门名称
    @GetMapping("/query")
    @ResponseBody

    public Map<String, Object> queryDepts(Dept dept, String likeParams, Integer pageNumber, Integer pageSize) {

        System.out.println(dept);
        System.out.println(likeParams);
        System.out.println(pageNumber);
        System.out.println(pageSize);

        Map<String, Object> params = new HashMap<>();
        params.put("dept", dept);
        params.put("likeParams", likeParams);

        PageHelper.startPage(pageNumber, pageSize);
        List<Dept> deptByParams = hrmsService.findDeptByParams(params);
        PageInfo pageInfo = new PageInfo(deptByParams);
        Map<String, Object> result = new HashMap<>();
        result.put("stauts", 200);
        result.put("data", pageInfo);
        result.put("message", "查询到了数据");
        return result;
    }


}
